package com.example.clothingmanager.controller;

import com.example.clothingmanager.bean.User;
import com.example.clothingmanager.service.UserService;
import com.example.clothingmanager.utils.ImageUtil;
import com.example.clothingmanager.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author Huangyt
 * @version 1.0
 * @date 2020/5/10 15:36
 */
@Component
@ApiOperation("用户管理")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @ApiOperation("添加用户")
    @PostMapping("/add")
    public Result add(User user, MultipartFile image, HttpServletRequest request) throws IOException {
        if(null!=user&&null!=image){
            Result res = userService.add(user);
            if(res.isSuccess()){
                saveOrUpdateImageFile(user,image, request);
                return res;
            }
            return res;
        }
        return Result.fail("数据传输不全");
    }

    @PutMapping("/update")
    public Result update(User user, MultipartFile image, HttpServletRequest request) throws IOException {
        if(null!=user){
            Result res = userService.update(user);
            if(res.isSuccess()){
                if(null!=image){
                    saveOrUpdateImageFile(user,image, request);
                }
                return res;
            }
            return res;
        }
        return Result.fail("数据传输不全");
    }

    @GetMapping("get/{id}")
    public Result get(@PathVariable("id") int id){
        return userService.get(id);
    }

    @GetMapping("/list/{page}/{size}")
    public Result list(@PathVariable("page")int page, @PathVariable("size")int size, HttpSession session){
        return userService.list(page, size, session);
    }

    @DeleteMapping("/del/{id}")
    public Result delete(@PathVariable("id")int id){
        deletePic(id);
        return userService.delete(id);
    }

    @GetMapping("/userlist")
    public Result userlist(){
        return userService.list();
    }

    public void deletePic(int id){
        //获取项目根路径
        String property = System.getProperty("user.dir");
        //添加根路径后的静态资源路径
        File imageFolder = new File(property + "\\src\\main\\resources\\static\\images\\user");
        //创建头像文件对象
        File file = new File(imageFolder,id + ".jpg");
        if(file.exists()){
            file.delete();
        }
    }

    public void saveOrUpdateImageFile(User user, MultipartFile image, HttpServletRequest request)
            throws IOException {
        //获取项目根路径
        String property = System.getProperty("user.dir");
        //添加根路径后的静态资源路径
        File imageFolder = new File(property + "\\src\\main\\resources\\static\\images\\user");
        //创建头像文件对象
        File file = new File(imageFolder,user.getId() + ".jpg");
        if(!file.getParentFile().exists())
            file.getParentFile().mkdirs();
        image.transferTo(file);
        BufferedImage img = ImageUtil.change2jpg(file);
        ImageIO.write(img, "jpg", file);
    }
}
